<?php defined('SYSPATH') or die('No direct script access.');
/**
 * controller class for automatic templating.
 * 控制器文件基类
 */
class Controller_Admin_Template extends Kohana_Controller_Template {

	/**
	 * Loads the UserInfo.
	 */
	public function before()
	{
		parent::before();

		$this->set_no_cache();
		//$this->filter_xss();
		$user = user::current_user();

		// 已登录用户, 设置用户信息
		//////////////////////////////////////////////////////////
		if ($user->is_login() && is_object($this->template))
		{
            $uinfo = user::current_user_info();
            $utype = Arr::get($uinfo, 'utype');
			$this->template->set('user_info', user::current_user_info());
            $this->template->set('user_messages_count', notice::factory('notification')->count(user::current_user_id()));
		}

		// 公共模板变量: global_site_type
        $this->load_public_notice();
		$this->template->set('global_site_type', site::type());
		$this->template->set('global_sub_domain', site::subdomain(-4));
		$this->template->set('global_is_passport_login', user::is_passport_login());
		$user_agent = Request::user_agent(array('browser', 'version'));
		$this->template->set('browser', array(
					'type'    => Arr::get($user_agent, 'browser', 'Internet Explorer'),
					'version' => Arr::get($user_agent, 'version', '8.0'),
				));

		switch (site::type())
		{
			case 'merchant' :
				break;

			case 'admin':
				break;

			case 'manager':
				$this->load_admin_header();
				break;

			default: 
				break;
		}

		return TRUE;
	}

	/*
	 * 载入商铺基本信息
	 */
	private function load_shop_info ()
	{
		if (User::is_login())
		{
			$shop_info = Shop::info(Shop::current_id());
			$this->template->set('header_shop_info', $shop_info);

			// 检测是否是店铺第一次登陆
			$first_access = $this->track_shop_first_access($shop_info);
			if ($first_access && $shop_info['type'] == 'free')
			{
				$events = Kohana::config('events.shop.first_access');
				$param = array(
					'shop_id' => Shop::current_id(),
					'action'  => 'first_access',
					'time'    => time(),
				);
				events::notify($events,$param);

			}
		}
	}

	//分页信息
    protected function pagination ($count, $limit = 10, $page_type = 'route', $page_key = 'param1', $views = 'pagination/floating')
    {
        $page_config = array(
            'current_page'      => array('source' => $page_type, 'key' => $page_key),
            'total_items'       => (int) $count,
            'items_per_page'    => (int) $limit,
            'view'              => $views,
        );

		return Pagination::factory($page_config)->render();
    }
	

	private function load_navigator ()
	{
		$this->template->set('common_navigator', Navigator::instance()->render());
	}

	private function load_admin_header()
	{
        //
	}

    //
    // 未防止前端代理或者某些网关缓存内容，这里强制设置不缓存
    //
	private function set_no_cache ()
	{
        header("Expires: Mon, 26 Jul 1970 05:00:00 GMT");
     	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
     	header("Cache-Control: no-cache, must-revalidate");
     	header("Pragma: no-cache");
	}

    //
    // xss安全过虑
    //
	private function filter_xss()
	{
		//is_initial
		if(!$this->request->is_initial())
			return false;

		//site
		if (site::type() != 'member')
			return false;

		$keys      = array();
		$xss_post  = array_keys($this->request->post());
		$xss_query = array_keys($this->request->query());
		$keys      = array_merge($xss_post,$xss_query);

		foreach($keys as $key)
		{
			$type  = FILTER_SANITIZE_STRING;
			$post  = Request::initial()->post($key);
			$query = Request::initial()->query($key);

			//此变量有值
			if(!empty($post))
				Request::initial()->post($key, Util::xssFilter($post, $type));
			if(!empty($query))
				Request::initial()->query($key, Util::xssFilter($query, $type));
		}
	}


    //
    // 全局通知
    //
    // 注：使用函数，以后通过该函数实现业务逻辑
    //
    private function load_public_notice ()
    {
        $this->template->set('public_notice', Kohana::config('message.public_notice'));
    }

} // End Controller_Template
